package com.example.imagesteganographylibrary.Text;


import com.example.imagesteganographylibrary.Text.AsyncTaskCallback.TextDecodingCallback;
import com.example.imagesteganographylibrary.Utils.ImageUtil;
import com.example.imagesteganographylibrary.Utils.MergePixelMap;
import com.example.imagesteganographylibrary.Utils.Utility;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.media.image.PixelMap;

import java.util.List;

/**
 * In this class all those method in EncodeDecode class are used to decode secret message in image.
 * All the tasks will run in background.
 */

public class TextDecoding {

    private ImageSteganography result;
    private TextDecodingCallback textDecodingCallback;
    TaskDispatcher taskDispatcher;

    /**
     * TextDecoding
     *
     * @param abilitySlice abilitySlice
     * @param textDecodingCallback    textDecodingCallback
     */
    public TextDecoding(Context abilitySlice, TextDecodingCallback textDecodingCallback) {
        super();
        this.textDecodingCallback = textDecodingCallback;
        this.result = new ImageSteganography();
        taskDispatcher = abilitySlice.getMainTaskDispatcher();
    }

    /**
     * TextDecoding
     *
     * @param abilitySlice abilitySlice
     */
    public TextDecoding(Context abilitySlice) {
        super();
        //making result object
        this.result = new ImageSteganography();
        taskDispatcher = abilitySlice.getMainTaskDispatcher();
    }

    /**
     * TextDecoding
     *
     * @param imageSteganographies imageSteganographies
     */
    public void doInBackground(ImageSteganography imageSteganographies) {
        textDecodingCallback.onStartTextEncoding();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ImageSteganography imageSteganography = imageSteganographies;
                //getting bitmap image from file
                PixelMap bitmap = imageSteganography.getImage();
                //splitting images
                List<MergePixelMap.PixelMapSlice> srcEncodedList = Utility.splitImage(bitmap);
                //decoding encrypted zipped message
                String decoded_message = EncodeDecode.decodeMessage(srcEncodedList);
                //text decoded = true
                if (Utility.isStringEmpty(decoded_message)) {
                    decoded_message = ImageUtil.getFileName();
                }

                if (!Utility.isStringEmpty(decoded_message)) {
                    result.setDecoded(true);
                }
                //decrypting the encoded message
                String decrypted_message = ImageSteganography.decryptMessage(decoded_message, imageSteganography.getSecret_key());
                //If decrypted_message is null it means that the secret key is wrong otherwise secret key is right.
                if (!Utility.isStringEmpty(decrypted_message)) {
                    //secret key provided is right
                    result.setSecretKeyWrong(false);
                    result.setMessage(decrypted_message);
                    for (MergePixelMap.PixelMapSlice bitm : srcEncodedList)
                        bitm.getPixelMap().release();
                }
                taskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        textDecodingCallback.onCompleteTextEncoding(result);
                    }
                });
            }
        }).start();
    }
}